part of 'cross_controller.dart';

class CrossPassController extends GetxController {
  final freq = 0.obs;
  final textEditingControllerFreq = TextEditingController();
  final slope = 0.obs;
  final xielv = 0.obs;
  final passType = PassType.high.obs;
  final focusNode = FocusNode();

  CrossPassController() {
    everAll([slope, xielv], (v) {
      Msg.msgXover(isHigh: isHigh);
    });

    debounce(freq, (v) {
      Msg.msgXover(isHigh: isHigh);
      textEditingControllerFreq.zftext = v.toString();
    }, time: kMsgInterval);
  }

  void updateFreq() {
    var freq = int.tryParse(textEditingControllerFreq.text) ?? 20;
    if (freq < 20) {
      freq = 20;
    }
    if (freq > 20000) {
      freq = 20000;
    }
    setFreq(freq);
  }

  ///是否高通
  var isHigh = true;

  XoverModel get model => DataMng().xoverModel;

  ///上次的数据
  XoverModel? lastModel;

  ///波特类型
  void setType(int type) {
    model.updateItem(isHigh: isHigh, type: type);
    this.xielv.value = type;
    refreshCurve();
  }

  ///更新斜率类型
  void setSlope({required int slope}) {
    model.updateItem(isHigh: isHigh, slope: slope);
    this.slope.value = slope;
    refreshCurve();
  }

  void setFreq(int freq) {
    model.updateItem(isHigh: isHigh, freq: freq);
    this.freq.value = freq;
    refreshCurve();
  }

  void increaseFreq() {
    int v = freq.value;
    v++;
    if (v > kFreqMax) {
      v = kFreqMax;
    }
    setFreq(v);
  }

  void decreaseFreq() {
    int v = freq.value;
    v--;
    if (v < kFreqMin) {
      v = kFreqMin;
    }
    setFreq(v);
  }

  void setPassType(PassType passType) {
    if (this.passType.value == passType) return;
    if (passType == PassType.high) {
      isHigh = true;
      if (lastModel != null) {
        DataMng().xoverModel = XoverModel.fromJson(lastModel!.toJson());
        lastModel = null;
      }
      model.updateItem(isHigh: isHigh, isBypass: false);

      Msg.msgXover2(selectCH: model.selectCH, isHigh: true);
    } else if (passType == PassType.low) {
      isHigh = false;
      if (lastModel != null) {
        DataMng().xoverModel = XoverModel.fromJson(lastModel!.toJson());
        lastModel = null;
      }
      model.updateItem(isHigh: isHigh, isBypass: false);

      Msg.msgXover2(selectCH: model.selectCH, isHigh: false);
    } else if (passType == PassType.all) {
      //bypass 曲线变平，数据
      lastModel = XoverModel.fromJson(model.toJson()); //备份上次数据
      model.updateItem(isBypass: true);

      // model.reset();//重置所有
      model.items[model.selectCH] = XoverItemModel();//重置当前通道

      // Msg.msgResetXover();
      Msg.msgXover2(selectCH: model.selectCH, isHigh: true);
      Msg.msgXover2(selectCH: model.selectCH, isHigh: false);
    }
    this.passType.value = passType;
  }

  ///重置所有通道
  void reset() {
    passType.value = isHigh ? PassType.high : PassType.low;
    model.reset();
    Get.find<CrossController>().selectCh.value = 0;
    Msg.msgResetXover();
    for (var i = 0; i < model.items.length; i++) {
      Msg.msgXover2(selectCH: i, isHigh: true);
      Msg.msgXover2(selectCH: i, isHigh: false);
    }
    refreshCurve();
  }

  ///重置当前通道
  void resetCurrent() {
    passType.value = isHigh ? PassType.high : PassType.low;
    final selectCH = model.selectCH;
    model.items[selectCH] = XoverItemModel();
    Msg.msgXover2(selectCH: selectCH, isHigh: true);
    Msg.msgXover2(selectCH: selectCH, isHigh: false);
    refreshCurve();
  }

  ///刷新曲线
  void refreshCurve() {
    MsgParser.done(sendData: false);
  }
}
